//
//  AddressManagerViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "AddressCell.h"
#import "AddressManagerViewController.h"
#import "AddAddressViewController.h"

@interface AddressManagerViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UIButton *addAddressBtn;

@property (nonatomic, strong) NSMutableArray <AddressItemModel *> *dataArray;

@end

@implementation AddressManagerViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    //在这里面刷新数据
    [self request_addressList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self.addAddressBtn pp_clipCorners:UIRectCornerAllCorners radius:self.addAddressBtn.frame.size.height/2];
    
}

- (void)request_AddressList {
    
}

- (void)request_AddressUpdateWithType:(ETTAddressActionType)type AndModel:(AddressItemModel *)model {

}

- (void)initView {
    
    self.title = @"地址管理";

    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.estimatedRowHeight = 0;
    
    self.tableview.backgroundColor = UIColorHex(0xf1f1f1);
    [self.tableview registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil] forCellReuseIdentifier:@"AddressCellID"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (IBAction)AddAddress:(id)sender {
    
    [self.navigationController pushViewController:[AddAddressViewController new] animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressCellID" forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    WEAKSELF
    cell.editBtnClickBlock = ^(AddressItemModel * _Nonnull model) {
    
//        //当前模型带入下一个vc以修改
        AddAddressViewController *addressEdit = [AddAddressViewController new];
        addressEdit.model = weakSelf.dataArray[indexPath.row];
        [weakSelf.navigationController pushViewController:addressEdit animated:YES];
    };
    
    //在cell更新状态后同时更新数据源
    cell.refreshItemModel = ^(AddressItemModel * _Nonnull model) {
        
        [self request_AddressUpdateWithType:ETTAddressActionType_SetDefault AndModel:model];
        
//        [self alert:[NSString stringWithFormat:@"收件人姓名：%@\n收件人手机号：%@\n已设为默认收货地址",model.addressName,model.addressPhone]];
    };
    
    cell.deleteBtnClickBlock = ^(AddressItemModel * _Nonnull model) {
        
    };

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.changeAddress) {
        self.changeAddress(self.dataArray[indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (NSMutableArray<AddressItemModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_addressList {
    
    NSMutableDictionary *dic = @{}.mutableCopy;

    NSString *url = [NSString stringWithFormat:@"%@%@?userId=%@",TT_Global_Api_Domain,API_AddressList,[TTUserModel sharedObject].userId];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
//                NSDictionary *dic = [data valueForKey:@"data"];
                NSArray *list = [data valueForKey:@"data"];
                if ([list isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        AddressItemModel *model = [AddressItemModel modelWithDictionary:listDic];
                        [weakSelf.dataArray addObject:model];
                    }
                    
                    if (weakSelf.dataArray.count == 0) {
                        if (weakSelf.tableview) {
                            [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoAddress btnTitle:@"" didPressButton:^{}];
                        }
                    }
                    [weakSelf.tableview reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

@end
